package com.ccmusic.dao.impl;

import java.util.List;

import com.ccmusic.dao.MatchingDao;
import com.ccmusic.entity.Matching;
import com.ccmusic.entity.Music;
import com.ccmusic.entity.MusicType;

/**
 * MatchingDao接口，对Dao操作的方法规范定义
 * 
 * @author 朱江洲
 *
 */
public class MatchingDaoImpl extends BaseDao implements MatchingDao {

	/**
	 * 新增用户-歌曲匹配
	 * 
	 * @param matching
	 *            用户和歌曲匹配的对象（对应关系：用户id-音乐id（表中的id））
	 * @param return
	 *            添加成功时返回true；添加失败返回false
	 */
	@Override
	public boolean addMacthing(Matching matching) {
		String sql = "insert into matching values(?,?)";
		int effectLines = this.execute(sql, matching.getUid(), matching.getMid());
		return effectLines > 0 ? true : false;
	}

	/**
	 * 通过用户id获取对应的所有匹配歌曲 用户登陆后获取自己的收藏歌单可用
	 * 
	 * @param uid
	 *            用户id
	 * @return 用户收藏（与用户id匹配）的所有歌曲id集合
	 */
	@SuppressWarnings("unchecked")
	@Override
	// 可以传String型的id，BaseDao里用了setObject()来设置参数，在发送到数据库之前，给定参数将被转换为相应 SQL 类型。
	// 同时方便了servlet，因为从jsp获取的参数都是String，免去了强转
	public List<Matching> getMatchingsByUid(String uid) {
		String sql = "select * from matching where uid=?";
		List<Matching> midList = (List<Matching>) this.select(sql, Matching.class, uid);
		return midList;
	}

	/**
	 * 已登录用户获取自己的歌单
	 * 默认歌曲mid=0获取mid>0的
	 */
	@Override
	public List<MusicType> getMusicCollectionByUid(String uid) {
		String sql = "select * from musictype where mid in (select mid from matching where uid=?) and mid > 0";
		List<MusicType> musicList = (List<MusicType>) this.select(sql, MusicType.class, uid);
		return musicList;
	}

	/**
	 * 删除用户所匹配的特定歌曲
	 * 
	 * @param matching
	 *            用户与该歌曲匹配关系对象
	 * @return 删除成功时返回true；删除失败时返回false
	 */
	@Override
	public boolean removeMatching(Matching matching) {
		String sql = "delete from matching where uid=? and mid=?";
		int effectLines = this.execute(sql, matching.getUid(), matching.getMid());
		return effectLines > 0 ? true : false;
	}

	@Override
	public boolean add(int uid, int mid) {
		String sql = "insert into matching values(?, ?)";
		int n = this.execute(sql, uid, mid);
		// List<Music> list = getMusciId();
		return n > 0 ? true : false;
	}

	public void getMusciId() {
		String sql = "select mid from music";
		List<Music> list = (List<Music>) this.select(sql, Music.class);
		System.out.println(list);
		for (int i = 0; i < list.size(); i++) {
			add(1, list.get(i).getMid());
		}
	}

	/**
	 * 根据音乐id删除用户所匹配的特定歌曲
	 * 
	 * @param mid
	 *            音乐id
	 * @return 删除成功时返回true；删除失败时返回false
	 */
	@Override
	public boolean removeMatchingByMid(String mid) {
		String sql = "delete from matching where mid=?";
		int rsCode = this.execute(sql, mid);
		return rsCode > 0;
	}

	@Override
	public boolean getMatching(int uid, int mid) {
		String sql="select * from matching where uid=? and mid=?";
		List<Matching> list=(List<Matching>) select(sql,Matching.class, uid, mid);
		return list.size()==0?false:true;
	}
	


}
